<template>
  <div>
    <div class="item-group-search">
      <!-- <input type="text" :autofocus="search" v-model="condition" @focus="serachFocus()" @blur="serachBlur()" /> -->
      <el-select v-model="condition" filterable clearable placeholder="请选择">
        <el-option v-for="(item, index) in displayData" :key="index" :label="item.name" :value="item.name" />
      </el-select>
    </div>
    <div class="item-group-items">
      <div v-for="(item, index) in displayData" :key="index" class="item-group-item">
        <el-card shadow="hover" @dblclick.native="$emit('explorerOpen',item,$event)">
          <icon-show class="item-group-item-icon" :src="item" />
          <span class="item-group-item-text">{{ item.name }}</span>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ItemGroup',
  components: {
    IconShow: () => import('@components/IconShow.vue')
  },
  props: {
    data: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      search: false,
      // condition: '请输入查询条件'
      condition: null
    }
  },
  computed: {
    displayData() {
      if (!this.search || !this.condition || !this.condition.trim()) {
        return this.data
      } else {
        return this.data.filter(item =>
          item.id.toLowerCase().indexOf(this.condition.trim().toLowerCase()) !== -1 ||
          item.name.toLowerCase().indexOf(this.condition.trim().toLowerCase()) !== -1
        )
      }
    }
  },
  methods: {
    serachFocus() {
      if (!this.search) {
        this.condition = ''
      }
      this.search = true
    },
    serachBlur() {
      if (!this.condition || !this.condition.trim()) {
        this.condition = '请输入查询条件'
        this.search = false
      }
    }
  }
}
</script>
<style scoped>
.item-group-search {
  padding: 1rem;
}
.item-group-items {
  display: flex;
  flex-flow: row wrap;
  padding: .5rem;
}
.item-group-item {
  flex-basis: 9rem;
  font-size: 1.5rem;
  text-align: center;
  margin: .5rem;
}
.item-group-item-icon {
  height: 6rem;
  line-height: 6rem;
}
.el-card {
  cursor: pointer;
}
.el-card /deep/ .el-card__body {
  padding: 1.2rem 0;
}
</style>
